package org.dromara.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.activity.domain.ActivityUser;
import org.dromara.activity.domain.bo.ActivityUserBo;
import org.dromara.activity.domain.vo.ActivityUserVo;
import org.dromara.activity.mapper.ActivityUserMapper;
import org.dromara.activity.service.ActivityUserService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
* @author Administrator
* @description 针对表【t_activity_user】的数据库操作Service实现
* @createDate 2024-01-16 09:59:09
*/
@Service
@RequiredArgsConstructor
public class ActivityUserServiceImpl extends ServiceImpl<ActivityUserMapper, ActivityUser>
    implements ActivityUserService {

    private final ActivityUserMapper baseMapper;

    @Override
    public TableDataInfo<ActivityUserVo> selectPageUserList(ActivityUserBo user, PageQuery pageQuery) {
        LambdaQueryWrapper<ActivityUser> lqw = buildQueryWrapper(user);
        Page<ActivityUserVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public int insertUser(ActivityUserBo bo) {
        ActivityUser user = MapstructUtils.convert(bo, ActivityUser.class);
        // 新增角色信息
        return baseMapper.insert(user);
    }

    @Override
    public int deleteUserByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int updateUserStatus(Integer id, Integer status) {
        return baseMapper.update(null,
            new LambdaUpdateWrapper<ActivityUser>()
                .set(ActivityUser::getStatus, status)
                .eq(ActivityUser::getId, id));
    }

    @Override
    public int updateUser(ActivityUserBo bo) {
        ActivityUser user = MapstructUtils.convert(bo, ActivityUser.class);
        // 修改学校信息
        return baseMapper.updateById(user);
    }



    private LambdaQueryWrapper<ActivityUser> buildQueryWrapper(ActivityUserBo bo) {
        LambdaQueryWrapper<ActivityUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNickname()), ActivityUser::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), ActivityUser::getTel, bo.getTel());
        lqw.orderByAsc(ActivityUser::getId);
        return lqw;
    }
}




